<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .div1{width: 400px; height: 400px; background-color: skyblue; position: relative;}
        .div2{width: 300px; height: 300px; background-color: green;
            position:absolute;
            left: 10px;
            top: 20px;
        }
    </style>
</head>
<body>
    <div class="div1">

        <div class="div2"></div>
    </div>
    <script>
        // 要获取的元素是相对于谁定位，获取上级定位元素
        // 每一个元素 如果没有上级定位元素 就是相对于body进行定位
        console.log(document.querySelector('.div2').offsetParent);
        // 获取偏移量
        console.log(document.querySelector('.div2').offsetLeft);
        console.log(document.querySelector('.div2').offsetTop);
    </script>
</body>
</html>